#include <stdio.h>
#include "io.h"
#include "sst25vf080_read.h"
#include "sst25vf080_write.h"

/*may try using X2 mode here to speed up processing*/
unsigned int determineStartPosn() //returns next empty position in memory,tested til posn 300 TESTED
{
	unsigned int  posn;
	unsigned char i;
	unsigned char j;
	unsigned char k;
	
	
	
	posn=0;
	for(i=0;i<255;i++)
	{
		for(j=0;j<255;j++)
		{
			for(k=0;k<255;k++)
			{
				//printf("%d\n\r",readByte(i,j,k));
				if((readByte(i,j,k))==255)
				{
					
					goto done;
				}
				else
				{
					
					posn++;
				}
			}
		}
	}
	printf("posn %d",posn);
	done:return (posn);
	
}
				
void writeFromAnyLocation(unsigned int location,int val)
{
	unsigned int posnCompare;
	unsigned char i;
	unsigned char j;
	unsigned char k;
	
	
	posnCompare=0;
	for(i=0;i<255;i++)
	{
		for(j=0;j<255;j++)
		{
			for(k=0;k<255;k++)
			{
				
				if(posnCompare==location)
				{	
					writeByte(i,j,k,val);
					goto done;
				}
				
				else
				{
					posnCompare++;
				}
			}
		}
	}
	
	done://end of function

	
	
}			
void writeFromStartPosn(int val) //writes at next location
{
	unsigned int posnCompare;
	unsigned int position;
	unsigned char i;
	unsigned char j;
	unsigned char k;
	
	position=determineStartPosn(); //gets empty position
	posnCompare=0;
	for(i=0;i<255;i++)
	{
		for(j=0;j<255;j++)
		{
			for(k=0;k<255;k++)
			{
				
				if(posnCompare==position)
				{	
					writeByte(i,j,k,val);
					goto done;
				}
				
				else
				{
					posnCompare++;
				}
			}
		}
	}
	
	done://end of function
	
}

unsigned int findIdentifier(unsigned int startPosn,int identifier) //seeks first location of what identifer required,offset from the location specified, TESTED
{
	unsigned int posn;
	unsigned char i;
	unsigned char j;
	unsigned char k;
	
	unsigned char i_Start; //stores location of offset
	unsigned char j_Start;
	unsigned char k_Start;

	
	
	/*offset to particular location*/
	posn=0;
	
	for(i_Start=0;i_Start<255;i_Start++)
	{
		for(j_Start=0;j_Start<255;j_Start++)
		{
			for(k_Start=0;k_Start<255;k_Start++)
			{
				
				
				if(posn==startPosn)
				{
					goto doneOffsetLocation;
				}
				else
				{
					posn++;
				}
			}
		}
	}
	
	
	
	
	doneOffsetLocation:posn=0;
	for(i=i_Start;i<255;i++)
	{
		for(j=j_Start;j<255;j++)
		{
			for(k=k_Start;k<255;k++)
			{
				if((readByte(i,j,k))==identifier)
				{
					goto done;
				}
				else
				{	
					posn++;
				}
			}
		}
	}
	
	done:return (startPosn+posn); //return position where identifier is stored
}


int readFromLoc(unsigned int location) /*reads from a location- TESTED*/ 
{
	
	unsigned int posn;
	unsigned char i_Start;
	unsigned char j_Start;
	unsigned char k_Start;
	
	
	/*offset to particular location*/
	posn=0;
	
	for(i_Start=0;i_Start<255;i_Start++)
	{
		for(j_Start=0;j_Start<255;j_Start++)
		{
			for(k_Start=0;k_Start<255;k_Start++)
			{
				
				
				if(posn==location)
				{
					goto doneSeeking2Location;
				}
				else
				{
					posn++;
				}
			}
		}
	}
	
	doneSeeking2Location:
	return readByte(i_Start,j_Start,k_Start);
	
}
	
	

	